<!--
  功能：考勤管理
  作者：licy
  邮箱：licy@briup.com
  时间：2020年12月07日 14:59:47
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='attendance'>
    <div class="btns">
      <div style="text-align:center;margin-bottom:.5em">
        <el-form inline size="small" >
          <el-form-item >
            <el-cascader filterable style="width:300px" v-model="param.channelId" clearable :options="channels" placeholder="请选择院校渠道"
          :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable v-model="param.dc" clearable :options="directionClazzTree" placeholder="请选择班级"
          :props="{ children:'clazzes',label:'name',value:'id'}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="param.attendanceDate" type="daterange" value-format="timestamp" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:right">
        <el-button size="small" type="primary" icon="el-icon-search" @click="loadStudents">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-download" @click="toDownload">导出</el-button>
      </div>
    </div>
    <el-table :data="studentData.list" size="small" v-loading="loading" border >
        <!-- <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column> -->
        <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
        <el-table-column prop="major" label="专业" width="200" align="center" fixed="left" />
        <el-table-column prop="realname" label="姓名" width="90" align="center"  />
        <el-table-column prop="direction" label="学习方向" align="center" width="180"></el-table-column>
        <!-- 
        <el-table-column prop="attendanceRate" label="出勤率" width="90" align="center"  >
          <template slot-scope="scope">
            <el-tag type="primary" size="mini" v-if="scope.row.attendanceRate === '100%'">{{scope.row.attendanceRate}}</el-tag>
            <el-tag type="danger" size="mini" v-else>{{scope.row.attendanceRate}}</el-tag>
          </template>
        </el-table-column> 
       
        <el-table-column prop="abnormalTimes" label="缺勤次数" width="90" align="center" >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="toAttendanceDetails(scope.row)">{{scope.row.abnormalTimes}}</el-button>
          </template>
        </el-table-column>
        -->
        <el-table-column min-width="300" label="考勤情况" >
          <template slot-scope="scope" >
            <div v-if="scope.row.attendances" @click="toAttendanceDetails(scope.row)">
              <div v-for='(atten,index) in scope.row.attendances' :key="index">
                {{atten}}
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column  label="基地名称" align="center" width="120">
          <template>
            杰普软件
          </template>
        </el-table-column>
      </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="studentData.total" 
          :page-size="studentData.pageSize" 
          :current-page="studentData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->

    <!-- 模态框 -->
    <el-dialog title="导出配置" :visible.sync="dialog_visible" width="30%" class="customer_modal">
      <div style="text-align:center;padding:20px 0">
        <el-button size="small" @click="download('current')">导出当前页</el-button>
        <el-button size="small" type="primary" @click="download('all')">全部导出</el-button>
      </div>
    </el-dialog>
    <!-- /模态框 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" title="缺勤详情" width="40%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-table size="small" :data="attendances">
          <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
          <el-table-column label="缺勤日期" align="center" width="100">
            <template slot-scope="scope">
              {{scope.row.attendanceDateStr}}
            </template>
          </el-table-column>
          <el-table-column label="缺勤阶段" align="center" width="80">
            <template slot-scope="scope">
              {{scope.row.attendanceStage}}
            </template>
          </el-table-column>
          <el-table-column label="缺勤状态" align="center" width="80">
            <template slot-scope="scope">
              {{scope.row.status}}
            </template>
          </el-table-column>
          <el-table-column label="缺勤原因" align="left" >
            <template slot-scope="scope">
              {{scope.row.comment}}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">关闭</el-button>
      </span>
    </Briupdrawer>
    <!-- /抽屉 -->
  </div>
</template>

<script>
import {get,post,postJSON,download} from '@/utils/request'
import _ from 'lodash'
import {getCalendar} from '@/utils/calendar'
import {mapGetters} from 'vuex'
import qs from 'qs'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
     tt:[],
     loading:false,
     dialog_visible:false,
     param:{
       page:1,
       pageSize:15
     },
     studentData:{list:[]},
     attendances:[],
     visible:false
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['directionClazzTree','channels'])
  },
  // 侦听器
  watch: {
   'param.dc':{
     handler(now){
       this.param.directionId = now[0];
       this.param.clazzId = now[1];
     }
   }
  },
  // 组件方法
  methods: {
    // 查看出勤详情
    toAttendanceDetails(row){
      let param = {
        startTime :this.param.attendanceDate[0],
        endTime : this.param.attendanceDate[1],
        studentId:row.id,
      }
      // 查询
      let url = "/studentAttendance/queryAttendances"
      get(url,param).then(({data} )=> {
        this.attendances = data;
        this.visible = true;
      });
    },
    // 导出学生考勤
    download(type){
      let param = _.clone(this.param);
      delete param.dc;
      if(param.attendanceDate){
        param.startTime = param.attendanceDate[0];
        param.endTime = param.attendanceDate[1];
        delete param.attendanceDate;
      }
      let url = "/studentAttendance/downloadStudentAttendance"
       switch(type){
        case "current":
          break;
        case "all":
          param.pageSize = 10000;
          break;
      }
      download(url,param).then(()=>{
        this.dialog_visible = false;
      });
    },
    // 打开模态框，选择如何导出
    toDownload(){
      this.dialog_visible = true;
    },
    // 初始化月份日历
    initTT (){
      let d = new Date();
      //当前月份
      this.tt = getCalendar(d.getFullYear(),d.getMonth());
      // 初始化查询日期
      this.param = {
        ...this.param,
        attendanceDate : [d.getTime()-(60*60*24*30*1000),d.getTime()]
      }
    },
    
    // 加载学生信息
    loadStudents(){
      this.loading = true;
      let url = '/studentAttendance/pageQueryAttendanceReport'
      let param = _.clone(this.param);
      delete param.dc;
      if(param.attendanceDate){
        param.startTime = param.attendanceDate[0];
        param.endTime = param.attendanceDate[1];
        delete param.attendanceDate;
      }
      get(url,param)
      .then(response => {
        this.studentData = response.data
        this.loading = false;
      })
    },
   
     // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadStudents();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadStudents();
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    this.initTT();  //初始化日历
    this.loadStudents();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style scoped>

</style>
